1
|
|
|
import { Inject } from '@nestjs/common'; |
2
|
|
|
import { CommandHandler } from '@nestjs/cqrs'; |
3
|
|
|
import { CreateLeaveRequestCommand } from './CreateLeaveRequestCommand'; |
4
|
|
|
import { ILeaveRequestRepository } from 'src/Domain/HumanResource/Leave/Repository/ILeaveRequestRepository'; |
5
|
|
|
import { LeaveRequest } from 'src/Domain/HumanResource/Leave/LeaveRequest.entity'; |
6
|
|
|
import { DoesLeaveRequestExistForPeriod } from 'src/Domain/HumanResource/Leave/Specification/DoesLeaveRequestExistForPeriod'; |
7
|
|
|
import { LeaveRequestAlreadyExistForThisPeriodException } from 'src/Domain/HumanResource/Leave/Exception/LeaveRequestAlreadyExistForThisPeriodException'; |
8
|
|
|
import { DoesLeaveExistForPeriod } from 'src/Domain/FairCalendar/Specification/DoesLeaveExistForPeriod'; |
9
|
|
|
import { EventsOrLeavesAlreadyExistForThisPeriodException } from 'src/Domain/FairCalendar/Exception/EventsOrLeavesAlreadyExistForThisPeriodException'; |
10
|
|
|
import { DoesLeaveRequestLackPostponedWorkedFreeDays } from 'src/Domain/HumanResource/Leave/Specification/DoesLeaveRequestLackPostponedWorkedFreeDays'; |
11
|
|
|
|
12
|
|
|
@CommandHandler(CreateLeaveRequestCommand) |
13
|
|
|
export class CreateLeaveRequestCommandHandler { |
14
|
|
|
constructor( |
15
|
|
|
@Inject('ILeaveRequestRepository') |
16
|
|
|
private readonly leaveRequestRepository: ILeaveRequestRepository, |
17
|
|
|
private readonly doesLeaveRequestExistForPeriod: DoesLeaveRequestExistForPeriod, |
18
|
|
|
private readonly doesLeaveExistForPeriod: DoesLeaveExistForPeriod, |
19
|
|
|
private readonly DoesLeaveRequestLackPostponedWorkedFreeDays: DoesLeaveRequestLackPostponedWorkedFreeDays |
20
|
|
|
) {} |
21
|
|
|
|
22
|
|
|
public async execute(command: CreateLeaveRequestCommand): Promise<string> { |
23
|
|
|
const { |
24
|
|
|
user, |
25
|
|
|
endDate, |
26
|
|
|
endsAllDay, |
27
|
|
|
type, |
28
|
|
|
startDate, |
29
|
|
|
startsAllDay |
30
|
|
|
} = command; |
31
|
|
|
|
32
|
|
|
let { comment } = command; |
33
|
|
|
|
34
|
|
|
if ( |
35
|
|
|
true === |
36
|
|
|
(await this.doesLeaveRequestExistForPeriod.isSatisfiedBy( |
37
|
|
|
user, |
38
|
|
|
startDate, |
39
|
|
|
endDate |
40
|
|
|
)) |
41
|
|
|
) { |
42
|
|
|
throw new LeaveRequestAlreadyExistForThisPeriodException(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
if ( |
46
|
|
|
true === |
47
|
|
|
(await this.doesLeaveExistForPeriod.isSatisfiedBy( |
48
|
|
|
user, |
49
|
|
|
startDate, |
50
|
|
|
endDate |
51
|
|
|
)) |
52
|
|
|
) { |
53
|
|
|
throw new EventsOrLeavesAlreadyExistForThisPeriodException(); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
if ( |
57
|
|
|
true === |
58
|
|
|
this.DoesLeaveRequestLackPostponedWorkedFreeDays.isSatisfiedBy( |
59
|
|
|
startDate, |
60
|
|
|
endDate |
61
|
|
|
) |
62
|
|
|
) { |
63
|
|
|
comment += ` ⚠️ Cette demande de congé contient un ou plusieurs jour(s) férié(s) glissant non pris en compte.`; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
const leaveRequest = await this.leaveRequestRepository.save( |
67
|
|
|
new LeaveRequest( |
68
|
|
|
user, |
69
|
|
|
type, |
70
|
|
|
startDate, |
71
|
|
|
startsAllDay, |
72
|
|
|
endDate, |
73
|
|
|
endsAllDay, |
74
|
|
|
comment |
75
|
|
|
) |
76
|
|
|
); |
77
|
|
|
|
78
|
|
|
return leaveRequest.getId(); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|